package edu.ccut.saturn.studio.logic;

import org.eclipse.gef.palette.CombinedTemplateCreationEntry;
import org.eclipse.gef.palette.ConnectionCreationToolEntry;
import org.eclipse.gef.palette.MarqueeToolEntry;
import org.eclipse.gef.palette.PaletteContainer;
import org.eclipse.gef.palette.PaletteDrawer;
import org.eclipse.gef.palette.PaletteGroup;
import org.eclipse.gef.palette.PaletteRoot;
import org.eclipse.gef.palette.PaletteSeparator;
import org.eclipse.gef.palette.PanningSelectionToolEntry;
import org.eclipse.gef.palette.ToolEntry;
import org.eclipse.gef.requests.CreationFactory;
import org.eclipse.gef.requests.SimpleFactory;
import org.eclipse.jface.resource.ImageDescriptor;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.logic.model.Connection;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnComponentNode;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnCreatComponentNode;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnDataMapping;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnDecisionNode;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnEndIterationNode;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnEndNode;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnInformationNode;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnPropertyNode;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnStartIterationNode;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnSwichNode;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnTransactionBeginNode;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnTransactionCommitNode;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnTransactionRollBackNode;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnWorkflowNodeInLogic;
import edu.ccut.saturn.studio.logic.model.saturn.logic.SaturnDecisionNodeInLogic;
import edu.ccut.saturn.studio.logic.model.saturn.logic.SaturnInformationNodeInLogic;
import edu.ccut.saturn.studio.logic.model.saturn.logic.SaturnLogicInLogic;
import edu.ccut.saturn.studio.logic.model.saturn.logic.SaturnStartNodeInLogic;

final class LogicEditorPaletteFactory {

	// line setting
	private static final String LOGIC_SOLID_LINE = Messages.getString("LogicEditorPaletteFactory.LinkLine");//"solid line";  //$NON-NLS-1$
	private static final String LOGIC_CREATE_A_SOLID_LINE_CONNECTION = Messages
			.getString("LogicEditorPaletteFactory.38"); //$NON-NLS-1$
	private static final String LOGIC_FALSE_LINE =Messages.getString("LogicEditorPaletteFactory.OffsetLinkLine"); //"false line"; //$NON-NLS-1$
	private static final String LOGIC_CREATE_A_FALSE_CONNECTION = Messages
			.getString("LogicEditorPaletteFactory.39"); //$NON-NLS-1$
	private static final String LOGIC_EXCEPTION_LINE = Messages.getString("LogicEditorPaletteFactory.ErrorLinkLine");//"exception line";  //$NON-NLS-1$
	private static final String LOGIC_CREATE_A_EXCEPTION_CONNECTION = Messages
			.getString("LogicEditorPaletteFactory.40"); //$NON-NLS-1$

	// tool setting
	private static final String LOGIC_TOOL = Messages
			.getString("LogicEditorPaletteFactory.41"); //$NON-NLS-1$
	private static final String LOGIC_DRAWER =Messages.getString("LogicEditorPaletteFactory.drawer"); //"drawer";  //$NON-NLS-1$
	private static final String LOGIC_BASIC_COMPONENT = Messages.getString("LogicEditorPaletteFactory.BasicComponent");//"basic component";  //$NON-NLS-1$

	// basic node setting
	private static final String LOGIC_START =Messages.getString("LogicEditorPaletteFactory.start"); //"start";  //$NON-NLS-1$
	private static final String LOGIC_START_NODE = Messages
			.getString("LogicEditorPaletteFactory.42"); //$NON-NLS-1$
	private static final String LOGIC_LOGIC =Messages.getString("LogicEditorPaletteFactory.logic"); //"logic";  //$NON-NLS-1$
	private static final String LOGIC_LOGIC_NODE = Messages
			.getString("LogicEditorPaletteFactory.43"); //$NON-NLS-1$
	private static final String LOGIC_COMPONENT =Messages.getString("LogicEditorPaletteFactory.component"); //"component";  //$NON-NLS-1$
	private static final String LOGIC_COMPONENT_NODE = Messages
			.getString("LogicEditorPaletteFactory.44"); //$NON-NLS-1$
	private static final String LOGIC_INFORMATION =Messages.getString("LogicEditorPaletteFactory.description"); //"information"; //$NON-NLS-1$
	private static final String LOGIC_INFORMATION_NODE = Messages
			.getString("LogicEditorPaletteFactory.45"); //$NON-NLS-1$
	private static final String LOGIC_DECISION =Messages.getString("LogicEditorPaletteFactory.choose");//"decision"; //$NON-NLS-1$
	private static final String LOGIC_DECISION_NODE = Messages
			.getString("LogicEditorPaletteFactory.46"); //$NON-NLS-1$
	private static final String LOGIC_END =Messages.getString("LogicEditorPaletteFactory.end"); //"end";  //$NON-NLS-1$
	private static final String LOGIC_END_NODE = Messages
			.getString("LogicEditorPaletteFactory.47"); //$NON-NLS-1$
	private static final String LOGIC_START_ITERATION =Messages.getString("LogicEditorPaletteFactory.StartIteration"); //"start iteration";  //$NON-NLS-1$
	private static final String LOGIC_START_ITERATION_NODE = Messages
			.getString("LogicEditorPaletteFactory.48"); //$NON-NLS-1$
	private static final String LOGIC_END_ITERATION = Messages.getString("LogicEditorPaletteFactory.EndIteration");//"end iteration";  //$NON-NLS-1$
	private static final String LOGIC_END_ITERATION_NODE = Messages
			.getString("LogicEditorPaletteFactory.49"); //$NON-NLS-1$
	private static final String LOGIC_TRANSACTION_BEGIN =Messages.getString("LogicEditorPaletteFactory.BeginTransaction"); //"transactionBegin"; //$NON-NLS-1$
	private static final String LOGIC_TRANSACTION_BEGIN_NODE = Messages
			.getString("LogicEditorPaletteFactory.50"); //$NON-NLS-1$
	private static final String LOGIC_TRANSACTION_COMMIT =Messages.getString("LogicEditorPaletteFactory.CommitTransaction"); //$NON-NLS-1$
	private static final String LOGIC_TRANSACTION_COMMIT_NODE = Messages
			.getString("LogicEditorPaletteFactory.51"); //$NON-NLS-1$
	private static final String LOGIC_TRANSACITON_ROLLBACK =Messages.getString("LogicEditorPaletteFactory.RollbackTransaction"); //"transactionRollback";  //$NON-NLS-1$
	private static final String LOGIC_TRANSACTION_ROLLBACK_NODE = Messages
			.getString("LogicEditorPaletteFactory.52"); //$NON-NLS-1$
	private static final String LOGIC_CREATE_COMPONENT =Messages.getString("LogicEditorPaletteFactory.CreateComponent");//"createComponent"; //$NON-NLS-1$
	private static final String LOGIC_CREATE_COMPONENT_NODE = Messages
			.getString("LogicEditorPaletteFactory.53"); //$NON-NLS-1$
	private static final String LOGIC_ATTR_VAL_ASSIGN_COMPONENT = Messages.getString("LogicEditorPaletteFactory.AttributeEvaluation");//"attributeEvaluationComponent";  //$NON-NLS-1$
	private static final String LOGIC_ATTR_VAL_ASSIGN_COMPONENT_NODE = Messages
			.getString("LogicEditorPaletteFactory.54"); //$NON-NLS-1$
	private static final String LOGIC_DATA_MAPPING =Messages.getString("LogicEditorPaletteFactory.DataMapping"); //"data mapping";  //$NON-NLS-1$
	private static final String LOGIC_DATA_MAPPING_NODE = Messages
			.getString("LogicEditorPaletteFactory.55"); //$NON-NLS-1$
	private static final String LOGIC_WORKFLOW = Messages.getString("LogicEditorPaletteFactory.workflow");//"work flow";  //$NON-NLS-1$
	private static final String LOGIC_WORKFLOW_NODE = Messages
			.getString("LogicEditorPaletteFactory.56"); //$NON-NLS-1$

	private static final String LOGIC_SWICH_NODE_NAME = Messages.getString("LogicEditorPaletteFactory.switch"); //$NON-NLS-1$
	private static final String LOGIC_SWICH_NODE = Messages.getString("LogicEditorPaletteFactory.10");  //$NON-NLS-1$

	private static PaletteRoot instance;

	static {
		instance = createPalette();
	}

	public static PaletteRoot getPaletteRoot() {
		return instance;
	}

	private static PaletteRoot createPalette() {
		PaletteRoot palette = new PaletteRoot();
		palette.add(createToolsGroup(palette));
		palette.add(createShapesDrawer());
		palette.add(createShapesDrawer1());
		return palette;
	}

	private static PaletteContainer createShapesDrawer() {
		PaletteDrawer componentsDrawer = new PaletteDrawer(LOGIC_DRAWER);

		CombinedTemplateCreationEntry startEntry = new CombinedTemplateCreationEntry(
				LOGIC_START, LOGIC_START_NODE, SaturnStartNodeInLogic.class,
				new SimpleFactory(SaturnStartNodeInLogic.class), ImageDescriptor
						.createFromFile(ShapesPlugin.class,
								Activator.START_MINI_NAME), ImageDescriptor
						.createFromFile(ShapesPlugin.class,
								Activator.START_NAME));

		CombinedTemplateCreationEntry componentEntry = new CombinedTemplateCreationEntry(
				LOGIC_COMPONENT, LOGIC_COMPONENT_NODE,
				SaturnComponentNode.class, new SimpleFactory(
						SaturnComponentNode.class), ImageDescriptor
						.createFromFile(ShapesPlugin.class,
								Activator.COMPONENT_MINI_NAME), ImageDescriptor
						.createFromFile(ShapesPlugin.class,
								Activator.COMPONENT_NAME));

		CombinedTemplateCreationEntry informationEntry = new CombinedTemplateCreationEntry(
				LOGIC_INFORMATION, LOGIC_INFORMATION_NODE,
				SaturnInformationNode.class, new SimpleFactory(
						SaturnInformationNodeInLogic.class), ImageDescriptor
						.createFromFile(ShapesPlugin.class,
								Activator.INFORMATION_MINI_NAME),
				ImageDescriptor.createFromFile(ShapesPlugin.class,
						Activator.INFORMATION_NAME));

		CombinedTemplateCreationEntry endEntry = new CombinedTemplateCreationEntry(
				LOGIC_END, LOGIC_END_NODE, SaturnEndNode.class,
				new SimpleFactory(SaturnEndNode.class), ImageDescriptor
						.createFromFile(ShapesPlugin.class,
								Activator.END_MINI_NAME), ImageDescriptor
						.createFromFile(ShapesPlugin.class,
								Activator.COMPONENT_NAME));

		CombinedTemplateCreationEntry logicEntry = new CombinedTemplateCreationEntry(
				LOGIC_LOGIC, LOGIC_LOGIC_NODE, SaturnLogicInLogic.class,
				new SimpleFactory(SaturnLogicInLogic.class), ImageDescriptor
						.createFromFile(ShapesPlugin.class,
								Activator.LOGIC_MINI_NAME), ImageDescriptor
						.createFromFile(ShapesPlugin.class,
								Activator.LOGIC_MANX_NAME));

		CombinedTemplateCreationEntry deciseionEntry = new CombinedTemplateCreationEntry(
				LOGIC_DECISION, LOGIC_DECISION_NODE, SaturnDecisionNode.class,
				new SimpleFactory(SaturnDecisionNodeInLogic.class),
				ImageDescriptor.createFromFile(ShapesPlugin.class,
						Activator.DECISION_MINI_NAME), ImageDescriptor
						.createFromFile(ShapesPlugin.class,
								Activator.DECISION_MANX_NAME));

		CombinedTemplateCreationEntry forstartEntry = new CombinedTemplateCreationEntry(
				LOGIC_START_ITERATION, LOGIC_START_ITERATION_NODE,
				SaturnStartIterationNode.class, new SimpleFactory(
						SaturnStartIterationNode.class), ImageDescriptor
						.createFromFile(ShapesPlugin.class,
								Activator.FOR_START_MINI_NAME), ImageDescriptor
						.createFromFile(ShapesPlugin.class,
								Activator.FOR_START_MAX_NAME));

		CombinedTemplateCreationEntry forendEntry = new CombinedTemplateCreationEntry(
				LOGIC_END_ITERATION, LOGIC_END_ITERATION_NODE,
				SaturnEndIterationNode.class, new SimpleFactory(
						SaturnEndIterationNode.class), ImageDescriptor
						.createFromFile(ShapesPlugin.class,
								Activator.FOR_END_MINI_NAME), ImageDescriptor
						.createFromFile(ShapesPlugin.class,
								Activator.FOR_END_MINI_NAME));

		CombinedTemplateCreationEntry transactionBeginEntry = new CombinedTemplateCreationEntry(
				LOGIC_TRANSACTION_BEGIN, LOGIC_TRANSACTION_BEGIN_NODE,
				SaturnTransactionBeginNode.class, new SimpleFactory(
						SaturnTransactionBeginNode.class), ImageDescriptor
						.createFromFile(ShapesPlugin.class,
								Activator.TRANSATIONBEGIN_NAME),
				ImageDescriptor.createFromFile(ShapesPlugin.class,
						Activator.TRANSATIONBEGIN_MAX_NAME));

		CombinedTemplateCreationEntry transactionCommitEntry = new CombinedTemplateCreationEntry(
				LOGIC_TRANSACTION_COMMIT, LOGIC_TRANSACTION_COMMIT_NODE,
				SaturnTransactionCommitNode.class, new SimpleFactory(
						SaturnTransactionCommitNode.class), ImageDescriptor
						.createFromFile(ShapesPlugin.class,
								Activator.TRANSATIONCOMMIT_NAME),
				ImageDescriptor.createFromFile(ShapesPlugin.class,
						Activator.TRANSATIONCOMMIT_MAX_NAME));

		CombinedTemplateCreationEntry transactionRollbackEntry = new CombinedTemplateCreationEntry(
				LOGIC_TRANSACITON_ROLLBACK, LOGIC_TRANSACTION_ROLLBACK_NODE,
				SaturnTransactionRollBackNode.class, new SimpleFactory(
						SaturnTransactionRollBackNode.class), ImageDescriptor
						.createFromFile(ShapesPlugin.class,
								Activator.TRANSATIONROLLBACK_NAME),
				ImageDescriptor.createFromFile(ShapesPlugin.class,
						Activator.TRANSATIONROLLBACK_MAX_NAME));

		CombinedTemplateCreationEntry swichEntry = new CombinedTemplateCreationEntry(
				LOGIC_SWICH_NODE_NAME, LOGIC_SWICH_NODE, SaturnSwichNode.class,
				new SimpleFactory(SaturnSwichNode.class), ImageDescriptor
						.createFromFile(ShapesPlugin.class, Activator.SWICH),
				ImageDescriptor.createFromFile(ShapesPlugin.class,
						Activator.SWICH_MAX));

		componentsDrawer.add(startEntry);
		componentsDrawer.add(logicEntry);
		componentsDrawer.add(componentEntry);
		componentsDrawer.add(informationEntry);
		componentsDrawer.add(deciseionEntry);
		componentsDrawer.add(endEntry);
		componentsDrawer.add(forstartEntry);
		componentsDrawer.add(forendEntry);
		componentsDrawer.add(transactionBeginEntry);
		componentsDrawer.add(transactionCommitEntry);
		componentsDrawer.add(transactionRollbackEntry);
		componentsDrawer.add(swichEntry);
		return componentsDrawer;
	}

	private static PaletteContainer createShapesDrawer1() {
		PaletteDrawer componentsDrawer = new PaletteDrawer(
				LOGIC_BASIC_COMPONENT);

		CombinedTemplateCreationEntry creatComponetEntry = new CombinedTemplateCreationEntry(
				LOGIC_CREATE_COMPONENT, LOGIC_CREATE_COMPONENT_NODE,
				SaturnCreatComponentNode.class, new SimpleFactory(
						SaturnCreatComponentNode.class), ImageDescriptor
						.createFromFile(ShapesPlugin.class,
								Activator.COMPONENT_MINI_NAME), ImageDescriptor
						.createFromFile(ShapesPlugin.class,
								Activator.COMPONENT_NAME));

		CombinedTemplateCreationEntry propertyEntry = new CombinedTemplateCreationEntry(
				LOGIC_ATTR_VAL_ASSIGN_COMPONENT,
				LOGIC_ATTR_VAL_ASSIGN_COMPONENT_NODE, SaturnPropertyNode.class,
				new SimpleFactory(SaturnPropertyNode.class), ImageDescriptor
						.createFromFile(ShapesPlugin.class,
								Activator.COMPONENT_MINI_NAME), ImageDescriptor
						.createFromFile(ShapesPlugin.class,
								Activator.COMPONENT_NAME));

		CombinedTemplateCreationEntry dataEntry = new CombinedTemplateCreationEntry(
				LOGIC_DATA_MAPPING, LOGIC_DATA_MAPPING_NODE,
				SaturnDataMapping.class, new SimpleFactory(
						SaturnDataMapping.class), ImageDescriptor
						.createFromFile(ShapesPlugin.class,
								Activator.DATAMAP_MIN_NAME), ImageDescriptor
						.createFromFile(ShapesPlugin.class,
								Activator.DATAMAP_MAX_NAME));

		CombinedTemplateCreationEntry workflowEntry = new CombinedTemplateCreationEntry(
				LOGIC_WORKFLOW, LOGIC_WORKFLOW_NODE,
				SaturnWorkflowNodeInLogic.class, new SimpleFactory(
						SaturnWorkflowNodeInLogic.class),
				ImageDescriptor.createFromFile(ShapesPlugin.class,
						Activator.WF_SMALL), ImageDescriptor.createFromFile(
						ShapesPlugin.class, Activator.WF_BIG));

		componentsDrawer.add(creatComponetEntry);
		componentsDrawer.add(propertyEntry);
		componentsDrawer.add(dataEntry);
		componentsDrawer.add(workflowEntry);
		return componentsDrawer;
	}

	private static PaletteContainer createToolsGroup(PaletteRoot palette) {
		PaletteGroup toolGroup = new PaletteGroup(LOGIC_TOOL);

		ToolEntry tool = new PanningSelectionToolEntry();
		toolGroup.add(tool);
		palette.setDefaultEntry(tool);
		toolGroup.add(new MarqueeToolEntry());
		toolGroup.add(new PaletteSeparator());

		tool = new ConnectionCreationToolEntry(LOGIC_SOLID_LINE,
				LOGIC_CREATE_A_SOLID_LINE_CONNECTION, new CreationFactory() {
					public Object getNewObject() {
						return null;
					}

					public Object getObjectType() {
						return Connection.SOLID_CONNECTION;
					}
				}, ImageDescriptor.createFromFile(ShapesPlugin.class,
						Activator.SOLID_CONNECTION_MIN), ImageDescriptor
						.createFromFile(ShapesPlugin.class,
								Activator.SOLID_CONNECTION_MAX));
		toolGroup.add(tool);

		tool = new ConnectionCreationToolEntry(LOGIC_FALSE_LINE,
				LOGIC_CREATE_A_FALSE_CONNECTION, new CreationFactory() {
					public Object getNewObject() {
						return null;
					}

					public Object getObjectType() {
						return Connection.DASHED_CONNECTION;
					}
				}, ImageDescriptor.createFromFile(ShapesPlugin.class,
						Activator.DASHED_CONNECTION_MIN), ImageDescriptor
						.createFromFile(ShapesPlugin.class,
								Activator.DASHED_CONNECTION_MAX));
		toolGroup.add(tool);

		tool = new ConnectionCreationToolEntry(LOGIC_EXCEPTION_LINE,
				LOGIC_CREATE_A_EXCEPTION_CONNECTION, new CreationFactory() {
					public Object getNewObject() {
						return null;
					}

					public Object getObjectType() {
						return Connection.EXCEPTION_CONNECTION;
					}
				}, ImageDescriptor.createFromFile(ShapesPlugin.class,
						Activator.SATURN_EXCEPTION_CONNECTION), ImageDescriptor
						.createFromFile(ShapesPlugin.class,
								Activator.SATURN_EXCEPTION_CONNECTION));
		toolGroup.add(tool);
		return toolGroup;
	}

	private LogicEditorPaletteFactory() {
	}
}